package tt.dz.service.imp.base;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tt.dz.dao.ext.common.OmsStationCollectDao;
import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.base.OmsCommonParam;
import tt.dz.entity.exception.OmsDaoException;
import tt.dz.entity.station.OmsStationCollect;
import tt.dz.service.base.StationCollectService;
import tt.dz.util.OmsConstant.MsgCodeEnum;
@Service @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class StationCollectServiceImpl extends BaseService implements StationCollectService{
	@Autowired
	private OmsStationCollectDao omsCommonDaoImpl;
	
	@Override
	public  synchronized JsonCommon<?> add(OmsStationCollect stationCollect) 
	{
		try 
		{
			String maxId = omsCommonDaoImpl.findMaxId(OmsStationCollect.class);
			maxId = genarateId(maxId,0);
			if (StringUtils.isNotEmpty(maxId))
			{
				stationCollect.setStation_id(maxId);
				omsCommonDaoImpl.add(stationCollect);
				return this.buildSuccessJsonCommon(stationCollect);
			}
		} catch (OmsDaoException e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
		return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:genarate id fail");
	}

	@Override
	public JsonCommon<?> update(OmsStationCollect stationCollect) 
	{
		if (StringUtils.isEmpty(stationCollect.getStation_id()))
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request id is empty");
		}
		try {
			omsCommonDaoImpl.update(stationCollect);
			return this.buildSuccessJsonCommon(stationCollect);
		} catch (OmsDaoException e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonCommon<?> delete(String id) {
		if (StringUtils.isEmpty(id))
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request id is empty");
		}
		try {
			omsCommonDaoImpl.deleteById(id, OmsStationCollect.class);
			return this.buildSuccessJsonCommon("success");
		} catch (OmsDaoException e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonCommon<?> findById(String id) {
		if (StringUtils.isEmpty(id))
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request id is empty");
		}
		try 
		{
			OmsStationCollect stationCollect = new OmsStationCollect();
			stationCollect.setStation_id(id);
			OmsStationCollect op = omsCommonDaoImpl.findRecordByPK(stationCollect);
			return this.buildSuccessJsonCommon(op);
		} 
		catch (Exception e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonForList<?> findPageObjs(OmsCommonParam param, OmsStationCollect stationCollect) 
	{
		try 
		{
			List<OmsStationCollect> lst = omsCommonDaoImpl.pagingFind(param, stationCollect);
			int total = 0;
			if (param.isQueryTotal())
			{
				total = omsCommonDaoImpl.queryTotalByObject(stationCollect);
			}
			return this.buildSuccessJsonForList(param, lst, total);
		} catch (Exception e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonForList();
		}
	}

	@Override
	public JsonCommon<?> addStationCollect2Station(String collectId, String stationId, String stationType,
			String entityId, String entityType, String userId) {
		if (StringUtils.isEmpty(collectId))
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request id is empty");
		}
		try {
			if ("1".equals(stationType))
			{
				omsCommonDaoImpl.addOmsStationByCollect(collectId, stationId, entityId, entityType, userId);;
			}
			else
			{
				omsCommonDaoImpl.addOmsStationExternalByCollect(collectId, stationId, userId);
			}
			OmsStationCollect stationCollect = new OmsStationCollect();
			stationCollect.setStation_id(collectId);
			stationCollect.setReview_state(OmsStationCollect.ReviewState.AuditPass.ordinal());
			omsCommonDaoImpl.update(stationCollect);
			return this.buildSuccessJsonCommon(stationCollect);
		} catch (OmsDaoException e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

}
